<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="8" :lg="8" :md="8" :sm="24" >
            <a-form-item label="任务发起人">
              <j-select-user-by-dep v-model="queryParam.creator"></j-select-user-by-dep>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="6" :md="6" :sm="24" >
            <a-form-item label="流程名称">
              <a-input v-model="queryParam.modelName" placeholder="请输入流程名称"/>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="6" :md="6" :sm="24" >
            <a-form-item label="流程编码">
              <a-input v-model="queryParam.modelKey" placeholder="请输入流程编码"/>
            </a-form-item>
          </a-col>
          <a-col :xl="4" :lg="4" :md="4" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button  type="primary" icon="search" @click="searchQuery">查询</a-button>
              <a-button type="primary" icon="reload" style="margin-left: 8px" @click="searchReset">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <!-- table区域-begin -->
    <div>
      <a-table
        size="middle"
        bordered
        row-key="id"
        :columns="columns"
        :data-source="dataSource"
        :pagination="ipagination"
        :loading="loading"
        @change="handleTableChange"
      >
        <template slot="action" slot-scope="text, record">
          <a v-if="record.taskType=== 1" @click="handleClick(record)">办理</a>
          <a-divider v-if="record.taskType=== 1" type="vertical" />
          <a v-if="record.taskType=== 1" @click="handleEntrustClick(record)">委托</a>
          <a-popconfirm v-if="record.taskType=== 0" title="确定拾取任务吗?" @confirm="() => handleClaimTask(record)">
            <a >拾取</a>
          </a-popconfirm>
          <a-divider v-if="record.taskType=== 0" type="vertical" />
          <a v-if="record.taskType=== 0" @click="handleDetails(record)">详情</a>
        </template>
      </a-table>
    </div>
    <!-- 任务办理弹窗 -->
    <task-modal ref="taskModal" :type="modalType" :taskData="taskData" :title="taskTitle" @ok="() => this.loadData(1)"></task-modal>
    <!-- 任务委托弹窗 -->
    <task-entrust-modal ref="taskEntrustModal" @ok="() => this.loadData()"></task-entrust-modal>
  </a-card>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import JSelectUserByDep from '@/components/jeecgbiz/JSelectUserByDep'
import taskModal from '../modal/TaskModal'
import { claimTask } from '@/api/task'
import JSelectMultiUser from '@/components/jeecgbiz/JSelectMultiUser'
import taskEntrustModal from '../modal/taskEntrustModal'
import STable from '@/components/table'
export default {
  name: 'myTask',
  components: {
    JSelectUserByDep,
    taskModal,
    JSelectMultiUser,
    taskEntrustModal,
    STable
  },
  mixins: [JeecgListMixin],
  data() {
    return {
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '业务标题',
          align: 'center',
          dataIndex: 'instanceName'
        },
        {
          title: '流程名称',
          align: 'center',
          dataIndex: 'modelName'
        },
        // {
        //   title: '流程编码',
        //   align: 'center',
        //   dataIndex: 'modelKey'
        // },
        {
          title: '发起人',
          align: 'center',
          dataIndex: 'creator'
        },
        {
          title: '开始时间',
          align: 'center',
          dataIndex: 'createTime'
        },
        {
          title: '当前环节',
          align: 'center',
          dataIndex: 'name'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/activiti/task/getMyAllTaskList'
      },
      queryParam: {
        type: '0,1'
      },
      excludeKeys: ['type', 'creator', 'modelName', 'modelKey'],
      // backVisible: false,
      taskData: {},
      form: this.$form.createForm(this),
      taskTitle: '',
      modalType: 'history_his'
    }
  },
  methods: {
    handleClick(data) {
      this.taskData = data
      this.taskTitle = '【' + data.modelName + '】流程办理'
      this.modalType = ''
      this.$refs.taskModal.editModalVisible = true
      // this.$refs.taskModal.show(data)
    },
    // handleBackCancel() {
    //   this.backVisible = false
    //   this.form.setFieldsValue({
    //     backComment: ''
    //   })
    // },
    // handleBackOk() { // 流程退回
    //   this.form.validateFields((err, values) => {
    //     if (!err) {
    //       backTask({
    //         comment: this.form.getFieldValue('backComment'),
    //         id: this.taskData.id,
    //         procInstId: this.taskData.processInstanceId,
    //         formDataId: this.taskData.formDataId
    //       }).then(res => {
    //         if (res.success) {
    //           this.$message.success('退回成功')
    //           this.loadData()
    //           this.backVisible = false
    //           this.form.setFieldsValue({
    //             backComment: ''
    //           })
    //         } else {
    //           this.$message.error(res.message)
    //         }
    //       }).catch((error) => {
    //         console.log(error)
    //       })
    //     }
    //   })
    // },
    handleEntrustClick(data) { // 点击委托
      this.$refs.taskEntrustModal.show(data.id)
    },
    // taskEntrust(data) {
    //   const that = this
    //   if (!data) {
    //     this.$message.error('请先选择委托人')
    //     return false
    //   }
    //   entrustTask({ taskId: this.taskData.id, userId: data }).then(function(res) {
    //     if (res.success) {
    //       that.$message.success('流程委托成功')
    //       that.loadData()
    //       that.$refs.taskEntrustModal.modalVisible = false
    //     } else {
    //       that.$message.error(res.message)
    //     }
    //   })
    // },
    handleDetails(data) {
      this.taskData = data
      this.taskTitle = '详情'
      this.modalType = 'history_his'
      this.$refs.taskModal.editModalVisible = true
    },
    handleClaimTask(record) { // 拾取流程
      claimTask({
        taskId: record.id
      }).then(res => {
        if (res.success) {
          this.loadData()
          this.$message.success(res.message)
        } else {
          this.$message.error(res.message)
        }
      })
    }
  }
}
</script>
